package org.smart4j.chapter2.util;

/**
 * 类型转换工具类
 * Created by Administrator on 2017/7/11.
 */
public class CastUtil {

    /**
     * 转换为字符串,默认值为空
     * @param obj
     * @return
     */
    public static String castString(Object obj){
        return castString(obj,"");
    }

    /**
     * 转换为字符串,可给默认值
     * @param obj
     * @param defaultValue
     * @return
     */
    public static String castString(Object obj,String defaultValue){
        return obj != null ? String.valueOf(obj) : defaultValue;
    }

    /**
     * 转换为int类型，默认为0
     * @param obj
     * @return
     */
    public static int castInt(Object obj){
        return castInt(obj,0);
    }

    /**
     * 转换为int类型，含默认值
     * @param obj
     * @param defaultValue
     * @return
     */
    public static int castInt(Object obj,int defaultValue){
        int value = defaultValue;
        if (obj !=null){//非空转换为字符串
           String str = String.valueOf(obj);
           if (StringUtil.isNotEmpty(str)){//非空和非空格转换为int类型
               try{
                   value = Integer.parseInt(str);
               }catch (NumberFormatException e){
                    value = defaultValue;
               }
           }
        }
        return value;
    }

    /**
     * 转换为布尔类型，默认值为false
     * @param obj
     * @return
     */
    public static boolean castBoolean(Object obj){
        return castBoolean(obj,false);//默认false
    }

    /**
     * 转换为布尔类型，含默认值
     * @param obj
     * @param defaultValue
     * @return
     */
    public static boolean castBoolean(Object obj,boolean defaultValue){
        boolean value = defaultValue;
        if(obj != null){
            value = Boolean.parseBoolean(castString(obj));
        }
        return value;
    }

    /**
     * 转换为double类型，默认值为0
     * @param obj
     * @return
     */
    public static double castDouble(Object obj){
        return castDouble(obj,0);
    }

    /**
     * 转换为double类型，含默认值
     * @param obj
     * @param defaultValue
     * @return
     */
    public static double castDouble(Object obj,double defaultValue){
        double value = defaultValue;
        if (obj != null){
            String str = String.valueOf(obj);
            if (StringUtil.isNotEmpty(str)){
                try {
                    value = Double.parseDouble(str);
                }catch (NumberFormatException e){
                    value = defaultValue;
                }
            }
        }
        return value;
    }

    /**
     * 转换为long类型，默认值0
     * @param obj
     * @return
     */
    public static long castLong(Object obj){
        return castLong(obj,0);
    }

    /**
     * 转换为long类型，含默认值
     * @param obj
     * @param defaultValue
     * @return
     */
    public static long castLong(Object obj,long defaultValue){
        long value = defaultValue;
        if(obj != null){
            String str = String.valueOf(obj);
            if (StringUtil.isNotEmpty(str)){
                try{
                    value = Long.parseLong(str);
                }catch (NumberFormatException e){
                    value = defaultValue;
                }
            }
        }
        return value;
    }

}
